package common

import (
	"net/http"
	"net/url"
	"strings"
)

type Config struct {
	limitShow         int
	lang              []string
	numLink, ellipsis bool
}

var (
	defaultC = Config{limitShow: 5, lang: []string{"首页", "上一页", "下一页", "尾页"}, numLink: true, ellipsis: false}
)

func PageGet(p map[string]int, tag1 string, tag2 string, nowShow bool) map[string]interface{} {
	a := make(map[string]interface{})
	a["start"] = 0
	a["end"] = 0
	aLink := make([]string, 0)
	if !ValueIsEmpty(p) {
		_, stotalOk := p["total"]
		sizetotal := 1
		if stotalOk {
			sizetotal = p["total"]
		}

		pageSize := sizetotal
		_, sizeOk := p["size"]
		if sizeOk {
			pageSize = p["size"]
		}

		_, totalOk := p["total"]
		total := 0
		if totalOk {
			total = p["total"]
		}

		_, pageOk := p["page"]
		nowPage := 1
		if pageOk {
			nowPage = p["page"]
		}
		if pageSize < 1 {
			pageSize = 1
		}

		a["start"] = (nowPage-1)*pageSize + 1
		a["end"] = nowPage * pageSize
		a["total"] = total
		a["size"] = pageSize // number_format($pagesize);
		allPage := ((total - 1) / pageSize) + 1
		a["pagecount"] = allPage
		a["nowPage"] = nowPage

		if nowPage-1 > 0 {
			if !ValueIsEmpty(defaultC.limitShow) && allPage > defaultC.limitShow && 1 < (nowPage-(defaultC.limitShow%2+(defaultC.limitShow/2))) {
				aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", "1", -1), "_NUM2_", defaultC.lang[0], -1))
			}
			aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", ValueToString((nowPage-1), "0"), -1), "_NUM2_", defaultC.lang[1], -1))
		} else if !nowShow {
			aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", "1", -1), "_NUM2_", defaultC.lang[0], -1))
			aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", "0", -1), "_NUM2_", defaultC.lang[1], -1))
		}

		if defaultC.numLink {
			if defaultC.ellipsis && !ValueIsEmpty(defaultC.limitShow) && allPage > defaultC.limitShow && 1 <= (nowPage-(defaultC.limitShow%2+(defaultC.limitShow/2))) {
				aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", ValueToString((nowPage-1), "0"), -1), "_NUM2_", "...", -1))
			}
			for i := 1; i <= allPage; i++ {
				tags1 := strings.Replace(strings.Replace(tag1, "_NUM1_", ValueToString(i, "0"), -1), "_NUM2_", ValueToString(i, "0"), -1)
				tags2 := strings.Replace(strings.Replace(tag2, "_NUM1_", ValueToString(i, "0"), -1), "_NUM2_", ValueToString(i, "0"), -1)
				if ValueIsEmpty(defaultC.limitShow) ||
					(nowPage <= (defaultC.limitShow%2+(defaultC.limitShow/2)) && i <= defaultC.limitShow) ||
					(i >= ((allPage-defaultC.limitShow)+1) && i <= (nowPage+(defaultC.limitShow%2+(defaultC.limitShow/2))-1)) ||
					(i >= (nowPage-(defaultC.limitShow%2+(defaultC.limitShow/2))+1) && i <= (nowPage+(defaultC.limitShow%2+(defaultC.limitShow/2))-1)) {
					if i == nowPage {
						aLink = append(aLink, tags2)
					} else {
						aLink = append(aLink, tags1)
					}
				}
			}
			if defaultC.ellipsis && !ValueIsEmpty(defaultC.limitShow) && allPage > defaultC.limitShow && nowPage < (allPage-(defaultC.limitShow%2+(defaultC.limitShow/2))+1) {
				aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", ValueToString((nowPage+1), "0"), -1), "_NUM2_", "...", -1))
			}
		}
		if nowPage+1 <= allPage {
			aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", ValueToString((nowPage+1), "0"), -1), "_NUM2_", defaultC.lang[2], -1))
			if !ValueIsEmpty(defaultC.limitShow) && allPage > defaultC.limitShow && nowPage < (allPage-(defaultC.limitShow%2+(defaultC.limitShow/2))+1) {
				aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", ValueToString(allPage, "0"), -1), "_NUM2_", defaultC.lang[3], -1))
			}
		} else if !nowShow {
			aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", "0", -1), "_NUM2_", defaultC.lang[2], -1))
			aLink = append(aLink, strings.Replace(strings.Replace(tag1, "_NUM1_", ValueToString(allPage, "0"), -1), "_NUM2_", defaultC.lang[3], -1))
		}
		a["link"] = aLink
		return a
	} else {
		return a
	}
}

func parseLink(para map[string]interface{}, urls string, host string, r *http.Request) string {
	if ValueIsEmpty(urls) {
		urls = r.RequestURI
	}
	reUrl := urls
	arr, err := url.Parse(urls)
	if !ValueIsEmpty(para) {
		if err != nil {
			return ""
		}
		addArr := make(map[interface{}]interface{}, 0)
		rep := ""
		qstr := ""
		if !ValueIsEmpty(arr.RawQuery) {
			qArr, _ := url.ParseQuery(arr.RawQuery)
			rQarr := qArr
			for k, v := range para {
				_, qArrOk := qArr[k]
				if qArrOk {
					if ValueIsEmpty(v) {
						delete(rQarr, k)
					} else {
						rQarr.Set(k, ValueToString(v, ""))
					}
				} else if k == "_PARA_" {
					rep = rep + ValueToString(v, "")
				} else {
					rQarr.Set(k, ValueToString(v, ""))
				}
			}
			for k, v := range addArr {
				rQarr.Add(ValueToString(k, ""), ValueToString(v, ""))
			}
			for k, v := range rQarr {
				qstr = qstr + ValueToString(k, "") + "=" + ValueToString(v, "") + "&"
			}
			qstr = strings.TrimRight(qstr, "&")
		} else {
			_, paraOk := para["_PARA_"]
			if paraOk {
				delete(para, "_PARA_")
			}
			for k, v := range para {
				qstr = qstr + ValueToString(k, "") + "=" + ValueToString(v, "") + "&"
			}
			qstr = strings.TrimRight(qstr, "&")
		}
		if !ValueIsEmpty(host) {
			arr.Host = host
			arr.Scheme = "http"
		}
		if !ValueIsEmpty(arr.Host) {
			reUrl = reUrl + arr.Scheme + "://"
			if !ValueIsEmpty(arr.User) {
				//reUrl = reUrl + arr.Uesr + ":" + arr.User.Password + "@"
			}
			reUrl = reUrl + arr.Host
		}
		reUrl = reUrl + arr.Path
		if !ValueIsEmpty(qstr) {
			reUrl = reUrl + "?" + qstr
		} else {
			if !ValueIsEmpty(para) {
				reUrl = reUrl + "?"
			} else {
				reUrl = reUrl + ""
			}
		}
		if !ValueIsEmpty(arr.Fragment) {
			reUrl = reUrl + "#" + arr.Fragment
		}
		if !ValueIsEmpty(rep) {
			strings.Replace(reUrl, "_PARA_", rep, -1)
		}
	}
	to1, err := url.QueryUnescape(reUrl)
	toStr, err := url.QueryUnescape(to1)
	temp := strings.ToUpper(toStr)
	if strings.Index(temp, "<") != -1 || strings.Index(temp, `"`) != -1 || strings.Index(temp, "'") != -1 || strings.Index(temp, "CONTENT-TRANSFER-ENCODING") != -1 {
		reUrl = "#"
	}
	return reUrl
}
